package com.wanqing.aspect;


import com.wanqing.annotation.AutoFill;
import com.wanqing.constant.AutoFillConstant;
import com.wanqing.context.BaseContext;
import com.wanqing.enumeration.OperationType;
import com.wanqing.exception.AutoFillException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    @Pointcut("execution(* com.wanqing.mapper.*.*(..))&&@annotation(com.wanqing.annotation.AutoFill)")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段自动填充");
        // 获取当前被拦截的方法上的注解对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();// 获取方法签名
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);// 获取方法上的注解对象
        OperationType operationType = autoFill.value();// 获取注解中的枚举值
        //准备赋值的数据
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        Object entity = args[0];
        Long currentId = BaseContext.getCurrentId();
        BaseContext.removeCurrentId();// 清理当前线程的id, 防止内存泄漏
        LocalDateTime now = LocalDateTime.now();
        // 根据当前的操作类型，为对应的实体对象中的属性进行值注入
        if (operationType == OperationType.INSERT) {
            log.info("开始进行插入操作的公共字段自动填充");
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setCreateTime.invoke(entity, now);
                setCreateUser.invoke(entity, currentId);
                setUpdateUser.invoke(entity, currentId);
                setUpdateTime.invoke(entity, now);
            } catch (Exception e) {
                throw new AutoFillException(e.getMessage());
            }
        }
        if (operationType == OperationType.UPDATE) {
            log.info("开始进行更新操作的公共字段自动填充");
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, currentId);
            } catch (Exception e) {
                throw new AutoFillException(e.getMessage());
            }
        }
    }
}
